<?php
class Membership extends AppModel {
    var $useDbConfig  = 'mssql';
    var $useTable     = 'Memberships';
    var $primaryKey   = 'MembershipID';
    var $displayField = 'GiveName';
    var $name         = 'Membership';

    public function getCurrentBvInfo ($ids) {
        $data = $this->find('all', array(
            'conditions' => array(
                'MembershipID' => $ids
            ),
            'recursive'  => -1,
            'fields'     => array('MembershipID', 'MemberCode', 'accuA', 'accuB', 'accuC', 'paidA', 'paidB', 'paidC', 'ForfeitA', 'ForfeitB', 'ForfeitC')
        ));
        $resp = array();

        if (count($data) == 0) {
            return 0;
        }

        foreach ($data as $m) {
            $id = $m['Membership']['MembershipID'];

            $sql = 'GetSponsorQty "'.$m['Membership']['MemberCode'].'"';
            $rs  = $this->query($sql);

            $resp[$id] = array(
                'AA' => $m['Membership']['accuA'],
                'BA' => $m['Membership']['accuB'],
                'CA' => $m['Membership']['accuC'],
                'AP' => $m['Membership']['paidA'],
                'BP' => $m['Membership']['paidB'],
                'CP' => $m['Membership']['paidC'],
                'AF' => $m['Membership']['ForfeitA'],
                'BF' => $m['Membership']['ForfeitB'],
                'CF' => $m['Membership']['ForfeitC'],
                'left'  => isset($rs[0][0]['LeftQty']) ? $rs[0][0]['LeftQty'] : 0,
                'right' => isset($rs[0][0]['RightQty']) ? $rs[0][0]['RightQty'] : 0
            );
        }

        return $resp;
    } // getCurrentBvInfo

    public function getMemberTree ($id) {
        $sql  = 'GetMemberTree "'.$id.'", "C"';
        $data = $this->query($sql);
        $resp = array();

        if (count($data) == 0) {
            return 0;
        }

        foreach ($data as $i=>$r) {
            $m = $r[0]['MemberCode'];

            if ($m) {
                $resp[$m] = array(
                    'root_id'          => $id,
                    'member_id'        => $r[0]['UplineID'],
                    'sponsor_id'       => $r[0]['SponsorID'],
                    'international_id' => $r[0]['Membercode'],
                    'name'             => $r[0]['GivenName'],
                    'country'          => $r[0]['Country'],
                    'level'            => $r[0]['treelevel'],
                    'placement'        => ($r[0]['LegNo'] == 1) ? 'A' : 'B'
                );
            }
        }

        return $resp;
    } // getMemberTree

    public function afterSave ($created) {
        if (!$created) { // it's only an update record
            return;
        }

        $upline_id = $this->field('MembershipID');
        $inter_id  = $this->field('MemberCode');

        if (!$inter_id) {
            return;
        }

        $id1 = $inter_id + 1;
        $id2 = $inter_id + 2;
        $sql = 'INSERT INTO Memberships (UplineID, MemberCode, RankLookupID, GiveName, Status) VALUES '.
               '('.$upline_id.', \''.$id1.'\', 1, \'Reserved\', \'A\'), '.
               '('.$upline_id.', \''.$id2.'\', 1, \'Reserved\', \'A\')';
        $this->query($sql);
    } // afterSave
} // Membership
?>